package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.config.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.JsonResult;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.basic.util.baiduUtil.DistanceUtil;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.pojo.loginInfo.LoginInfo;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
//Propagation.SUPPORTS:支持别人事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg, SearchMasterMsgQuery> implements ISearchMasterMsgService {

    @Resource
    private ShopMapper shopMapper;
    @Resource
    private SearchMasterMsgMapper msgMapper;



    @Override
    public PageList<SearchMasterMsg> findPage(SearchMasterMsgQuery messageQuery) {
        //1.从redis里面获取店铺id
        Long shopId = LoginInfoUtil.getShopId();
        //2.去xml查询数据
        messageQuery.setShopId(shopId);
        List<SearchMasterMsg> list = msgMapper.findPage(messageQuery);
        if(list.size()==0){
            return new PageList<>();
        }
        //2.查询到店铺信息-
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        //通过百度的工具类进行5km过滤
        //filter:过滤  item:list的实例对象 条件:店铺地址和宠物发布地址5km内判断 collect:收集成一个list
        List<SearchMasterMsg> resultList = list.stream().filter(item -> DistanceUtil.calTwoAddressOfDistance(shop.getAddress(), item.getAddress()) < 05000).collect(Collectors.toList());
        //分页  limit  skip:跳过  limit:分页
        if(resultList.size()==0){
            return new PageList<>();
        }
        for (SearchMasterMsg searchMasterMsg : resultList) {
            if(searchMasterMsg.getAddress()==null){
                throw new BusinessException("请输入地址");
            }
        }
        List<SearchMasterMsg> collect = resultList.stream().skip((messageQuery.getPage() - 1) * messageQuery.getSize()).limit(messageQuery.getSize()).collect(Collectors.toList());
        //4.返回自定义分页对象
        return new PageList<SearchMasterMsg>((long) resultList.size(),collect);


    }

}
